﻿using Autofac.Extras.DynamicProxy;
using Newtonsoft.Json.Linq;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Truron.Purchase.Interface;
using Truron.Purchase.Model;
using Truron.Purchase.Model.RemoteModel;
using Truron.Purchase.Service.DbContext;
using Truron.Purchase.Utility;
using Truron.Purchase.Utility.RedisHelper;

namespace Truron.Purchase.Service
{
    public class Purchase_LeftGroupService: IPurchase_LeftGroupService
    {
        private List<Purchase_LeftGroup> Init(bool isInit = false)
        {
            try
            {
                if (isInit)//初始化
                    RedisBase.Hash_Remove(StaticConstraint.PurchaseLeftGroupKey);
                var List = RedisBase.Hash_GetAll<Purchase_LeftGroup>(StaticConstraint.PurchaseLeftGroupKey);
                
                return List==null? new List<Purchase_LeftGroup>():List;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"初始化单据数据失败,isInit:{isInit},详细:{ex.ToString()}");
            }
            return new List<Purchase_LeftGroup>();
        }

        public Purchase_LeftGroup GetLeftGroup(string key, UserCache userCache)
        {
            return Init().Find(x => x.Key == key && x.UserId == userCache.UserId);
        }
        public void SaveLeftGroup(Purchase_LeftGroup entity, UserCache userCache)
        {
            RedisBase.Hash_Remove(StaticConstraint.PurchaseLeftGroupKey, entity.Key+userCache.UserId);
            RedisBase.Hash_Set(StaticConstraint.PurchaseLeftGroupKey, entity.Key + userCache.UserId, entity);
        }
    }
}
